package com.ewzj.bigdata.mvp.ui.adapter;

import android.content.Intent;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.ewzj.bigdata.R;
import com.ewzj.bigdata.app.constants.AppConstants;
import com.ewzj.bigdata.mvp.model.entity.resbean.NewBean;
import com.ewzj.bigdata.mvp.ui.activity.ArticleDetailActivity;
import com.ewzj.bigdata.mvp.ui.activity.VideoDetailActivity;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.http.imageloader.glide.ImageConfigImpl;
import com.jess.arms.utils.ArmsUtils;
import com.yqritc.recyclerviewflexibledivider.VerticalDividerItemDecoration;

import java.util.List;

import cn.jzvd.JZVideoPlayer;
import cn.jzvd.JZVideoPlayerStandard;

/**
 * Author:zhangjing
 * Time:2018/1/30  下午2:08
 * Name: this is NewsAdapter
 * Email:zhangjingworkmol@163.com
 */

public class NewsAdapter extends BaseMultiItemQuickAdapter<NewBean,BaseViewHolder>{
    /**
     *
     * 0、纯文字 1、单图 2、多图 3、大图 4、视频 5、专题  6、广告
     */
    int TYPE_LEVEL_0=0;
    int TYPE_LEVEL_1=1;
    int TYPE_LEVEL_2=2;
    int TYPE_LEVEL_3=3;
    int TYPE_LEVEL_4=4;
    int TYPE_LEVEL_5=5;


    private ImageLoader imageLoader;
    public NewsAdapter( @Nullable List<NewBean> data) {
        super(data);
        addItemType(TYPE_LEVEL_0,R.layout.item_news_quick);
        addItemType(TYPE_LEVEL_1,R.layout.item_news_oneimg);
        addItemType(TYPE_LEVEL_2,R.layout.item_news_manyimg);
        addItemType(TYPE_LEVEL_3,R.layout.item_news_onebigimg);
        addItemType(TYPE_LEVEL_4,R.layout.item_news_video);
        addItemType(TYPE_LEVEL_5,R.layout.item_news_topic);
    }

    @Override
    protected void convert(BaseViewHolder helper, NewBean item) {
        helper.getConvertView().setTag(helper);
        if (helper.getItemViewType()!=0&&helper.getItemViewType()!=5){
            helper.<TextView>getView(R.id.tv_newtitle).setText(item.getTitle());
            helper.<TextView>getView(R.id.tv_watch).setText(item.getVisit_num()+"");
            helper.<TextView>getView(R.id.tv_comment).setText(item.getComment_num()+"");
            if (imageLoader==null){
                imageLoader= ArmsUtils.obtainAppComponentFromContext(mContext).imageLoader();
            }
        }
        if (helper.getLayoutPosition()==mData.size()){
            helper.getView(R.id.view_line).setVisibility(View.INVISIBLE);
        }else {
            helper.getView(R.id.view_line).setVisibility(View.VISIBLE);
        }
        switch (helper.getItemViewType()){
            case 0:
                helper.<TextView>getView(R.id.tv_newtitle).setText(item.getTitle());
                helper.<TextView>getView(R.id.tv_source).setText(item.getSource());
                helper.<TextView>getView(R.id.tv_timeinfo).setText(item.getTime());
                helper.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent=new Intent(mContext,ArticleDetailActivity.class);
                        intent.putExtra(AppConstants.EXTRA_DATA,item);
                        ArmsUtils.startActivity(intent);
                    }
                });
                break;
            case 1:
                helper.<TextView>getView(R.id.tv_newdesc).setText(item.getSummary());
                if (item.getThumb()!=null&&item.getThumb().size()>0){
                    imageLoader.loadImage(mContext, ImageConfigImpl.builder().imageView(helper.<ImageView>getView(R.id.iv_newimg))
                            .url(item.getThumb().get(0))
                            .centerCrop(new CenterCrop())
                            .transformation(new RoundedCorners(10)).build());
                }
                if (TextUtils.isEmpty(item.getTag())){
                    helper.<TextView>getView(R.id.tv_tag).setVisibility(View.GONE);
                }else {
                    helper.<TextView>getView(R.id.tv_tag).setText(item.getTag());
                }

                helper.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent=new Intent(mContext,ArticleDetailActivity.class);
                        intent.putExtra(AppConstants.EXTRA_DATA,item);
                        ArmsUtils.startActivity(intent);
                    }
                });
                break;
            case 2:
                RecyclerView recyclerView=helper.<RecyclerView>getView(R.id.rv_newimgs);
                LinearLayoutManager linearLayoutManager=new LinearLayoutManager(mContext);
                linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
                if (recyclerView.getLayoutManager()==null){
                    recyclerView.setLayoutManager(linearLayoutManager);
                    recyclerView.addItemDecoration(new VerticalDividerItemDecoration.Builder(mContext)
                            .size(20)
                            .colorResId(R.color.white)
                            .build());
                }
                ImageAdapter imageAdapter=new ImageAdapter(item.getThumb());
                recyclerView.setAdapter(imageAdapter);
                if (TextUtils.isEmpty(item.getTag())){
                    helper.<TextView>getView(R.id.tv_tag).setVisibility(View.GONE);
                }else {
                    helper.<TextView>getView(R.id.tv_tag).setText(item.getTag());
                }
                helper.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent=new Intent(mContext,ArticleDetailActivity.class);
                        intent.putExtra(AppConstants.EXTRA_DATA,item);
                        ArmsUtils.startActivity(intent);
                    }
                });
                break;
            case 5:
                helper.<TextView>getView(R.id.tv_topicname).setText(item.getTitle());
                helper.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ArmsUtils.snackbarText("专题列表页面");
                    }
                });
                break;
            case 3:
//                helper.<TextView>getView(R.id.tv_newdesc).setText(item.getSummary()==null?"":item.getSummary());
                if (item.getThumb()!=null&&item.getThumb().size()>0){
                    imageLoader.loadImage(mContext, ImageConfigImpl.builder().imageView(helper.<ImageView>getView(R.id.iv_newimg))
                            .url(item.getThumb().get(0))
                            .centerCrop(new CenterCrop())
                            .transformation(new RoundedCorners(10)).build());
                }
                helper.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent=new Intent(mContext,ArticleDetailActivity.class);
                        intent.putExtra(AppConstants.EXTRA_DATA,item);
                        ArmsUtils.startActivity(intent);
                    }
                });
                break;
            case 4:
//                JZVideoPlayer.releaseAllVideos();
                JZVideoPlayerStandard jzVideoPlayer=helper.<JZVideoPlayerStandard>getView(R.id.videoplayer);
                jzVideoPlayer.setUp(
                       item.getVideo(), JZVideoPlayer.SCREEN_WINDOW_LIST,
                        "");
                if (item.getThumb()!=null&&item.getThumb().size()>0){
                    jzVideoPlayer.thumbImageView.setBackgroundColor(Color.WHITE);
                    imageLoader.loadImage(mContext, ImageConfigImpl.builder().imageView(jzVideoPlayer.thumbImageView)
                            .url(item.getThumb().get(0))
                            .centerCrop(new CenterCrop())
                            .transformation(new RoundedCorners(10)).build());
                }
                helper.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent=new Intent(mContext,ArticleDetailActivity.class);
                        intent.putExtra(AppConstants.EXTRA_DATA,item);
                        ArmsUtils.startActivity(intent);
                    }
                });
//                jzVideoPlayer.startButton.performClick();
                break;
        }

    }
}
